package com.base.vistter.iframe.service;

import com.base.vistter.iframe.model.location.LocationResponse;
import com.base.vistter.iframe.properties.IframeProperties;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

@Slf4j
@Service
@Transactional
public class LocationService {

    public static final String IP_GET_LOCATION = "https://apis.map.qq.com/ws/location/v1/ip";

    @Resource(name = "restTemplate")
    private RestTemplate restTemplate;

    @Resource(name = "iframeProperties")
    private IframeProperties iframeProperties;

    public LocationResponse getLocation(String ip){
        StringBuffer url = new StringBuffer();
        url.append(IP_GET_LOCATION).append("?key=").append(iframeProperties.getKey()).append("&ip=").append(ip);
        ResponseEntity<LocationResponse> response = restTemplate.getForEntity(url.toString(), LocationResponse.class);
        return response.getBody();
    }

}
